﻿Imports System.Data.SqlClient

Public Class frmAsignacionBiometria

    Private Sub frmAsignacionBiometria_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaEmpleados()
        CargaUsuarios()
        txtEmpleados.Focus()
        ToolStripButton4.Text = "Esperando..."

    End Sub
    Sub CargaEmpleados()
        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = CitraConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn

        strSql = "SELECT NombreCompleto FROM Empleados WHERE EstadoEmpleado<>'B'"


        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            txtEmpleados.AutoCompleteCustomSource.Add(rdBuscar("NombreCompleto").ToString.Trim)
        Loop
    End Sub

    Sub CargaUsuarios()

        lstUsuarios.Items.Clear()

        Dim cnconn As New SqlConnection
        cnconn.ConnectionString = InitialConnection
        Dim strSql As String = ""
        cnconn.Open()
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnconn

        strSql = "SELECT Nombre FROM Usuarios WHERE IdUsuario >1"


        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        rdBuscar = cmdBuscar.ExecuteReader

        Dim IdBiometria As Integer

        Do While rdBuscar.Read()
            IdBiometria = BuscarIntDatoCitra("SELECT IdBiometria FROM AsignacionBiometria WHERE NombreUsuario='" & rdBuscar("Nombre").ToString.Trim & "'")
            If IdBiometria = 0 Then
                lstUsuarios.Items.Add(rdBuscar("Nombre").ToString.Trim)
            End If
        Loop
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub txtEmpleados_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmpleados.Leave
        BuscarUsuarioAsignado()
    End Sub

    Private Sub txtEmpleados_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmpleados.TextChanged

    End Sub
    Sub BuscarUsuarioAsignado()
        Dim IdEmpleado As Integer = BuscarIntDatoCitra("SELECT IdEmpleado FROM Empleados WHERE UPPER(NombreCompleto)='" & UCase(txtEmpleados.Text.Trim) & "'")
        Dim IdBiometria As Integer = BuscarIntDatoCitra("SELECT IdBiometria FROM AsignacionBiometria WHERE IdEmpleado=" & IdEmpleado)

        If IdBiometria > 0 Then
            Dim Usuario As String = BuscarStrDatoCitra("SELECT NombreUsuario FROM AsignacionBiometria WHERE IdBiometria=" & IdBiometria)
            lstUsuarios.Items.Add(Usuario.Trim)
            lstUsuarios.SelectedItem = Usuario.Trim
            lstUsuarios.Enabled = False
            ToolStripButton4.Text = "Desasignar"
        Else
            lstUsuarios.SelectedIndex = 0
            lstUsuarios.Enabled = True
            ToolStripButton4.Text = "Asignar"
        End If
    End Sub
   

    Private Sub lstUsuarios_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsuarios.SelectedIndexChanged


    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        If ToolStripButton4.Text = "Asignar" Then
            Asignar()
        ElseIf ToolStripButton4.Text = "Desasignar" Then
            Desasignar()
        Else
            MsgBox("Seleccionar datos", MsgBoxStyle.Exclamation, "Aviso")
        End If
    End Sub
    Sub Asignar()
        If txtEmpleados.Text = "" Then
            MsgBox("Debes seleccionar un empleado", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If lstUsuarios.Text = "" Then
            MsgBox("Debes seleccionar un nombre de usuario", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim IdBiometria As Integer = BuscarIntDatoCitra("SELECT IdBiometria FROM AsignacionBiometria WHERE NombreUsuario='" & lstUsuarios.Text & "'")
        If IdBiometria > 0 Then
            MsgBox("El usuario seleccionado ya ha sido asignado", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim User As New clsAsignacionBiometria

        User.IdEmpleado = BuscarIntDatoCitra("SELECT IdEmpleado FROM Empleados WHERE UPPER(NombreCompleto)='" & UCase(txtEmpleados.Text.Trim) & "'")
        User.NombreUsuario = lstUsuarios.Text.Trim
        User.Insertar()

        txtEmpleados.Clear()
        lstUsuarios.SelectedIndex = 0
        ToolStripButton4.Text = "Esperando..."

        CargaUsuarios()
    End Sub
    Sub Desasignar()
        If txtEmpleados.Text = "" Then
            MsgBox("Debes seleccionar un empleado", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Call SetSqlCitra("DELETE FROM AsignacionBiometria WHERE NombreUsuario='" & lstUsuarios.Text.Trim & "'")

        lstUsuarios.Enabled = True
        txtEmpleados.Clear()
        lstUsuarios.SelectedIndex = 0
        ToolStripButton4.Text = "Esperando..."
        CargaUsuarios()

    End Sub
End Class